package com.zhixiang.xaec.dao.system.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.zhixiang.core.dao.impl.BaseDaoImpl;
import com.zhixiang.xaec.dao.system.AppRoleDao;
import com.zhixiang.xaec.entity.system.AppFunction;
import com.zhixiang.xaec.entity.system.AppRole;
import com.zhixiang.xaec.entity.system.FunUrl;

@Repository
@SuppressWarnings({"rawtypes","unchecked"})
public class AppRoleDaoImpl extends BaseDaoImpl<AppRole,Long>
  implements AppRoleDao
{


  public AppRole getByRoleName(String paramString)
  {
    String str = "FROM AppRole ar WHERE ar.roleName=?";
    return (AppRole)findUnique(str, new Object[] { paramString });
  }

  public HashMap<String, Set<String>> getSecurityDataSource()
  {
    final HashMap localHashMap = new HashMap();
    getHibernateTemplate().execute(new HibernateCallback()
    {
      public Object doInHibernate(Session paramAnonymousSession)
        throws HibernateException, SQLException
      {
        String str = "FROM AppRole";
        Query localQuery = paramAnonymousSession.createQuery(str);
        List localList = localQuery.list();
        Iterator localIterator1 = localList.iterator();
        while (localIterator1.hasNext())
        {
          AppRole localAppRole = (AppRole)localIterator1.next();
          TreeSet localTreeSet = new TreeSet();
          Iterator localIterator2 = localAppRole.getFunctions().iterator();
          while (localIterator2.hasNext())
          {
            AppFunction localAppFunction = (AppFunction)localIterator2.next();
            Iterator localIterator3 = localAppFunction.getFunUrls().iterator();
            while (localIterator3.hasNext())
              localTreeSet.add(((FunUrl)localIterator3.next()).getUrlPath());
          }
          localHashMap.put(localAppRole.getName(), localTreeSet);
        }
        return null;
      }
    });
    return localHashMap;
  }
}


